package com.passive.lock.controller.log;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.passive.lock.model.vo.PageInfo;
import com.passive.lock.model.vo.TDeviceLogVo;
import com.passive.lock.model.vo.TUserVo;
import com.passive.lock.service.log.DeviceLogService;
import com.passive.lock.utils.Config;
import com.passive.lock.utils.PageUtil;
import com.passive.lock.utils.ResourcesUtil;
import com.passive.lock.utils.Result;
@Controller
@RequestMapping("de")
public class DeviceLogController {
	@Autowired
	private DeviceLogService deviceLogService;
	
	@RequestMapping(value = "/deviceloglist")
	@ResponseBody
	public Result deviceLogList(@RequestParam(required = false) long beginTime,
			@RequestParam(required = false) long endTime, @RequestParam(required = false) String logType, @RequestParam(required = false) String username,
			@RequestParam(required = false) String lockname,@RequestParam(required = false) String keyname,@RequestParam Integer page, @RequestParam Integer limit, HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		String groupid= tUserVo.getGroupid();
		try {
			int offset = PageUtil.getOffset(page, limit);
			List<TDeviceLogVo> list = deviceLogService.deviceLogList(groupid,beginTime, endTime, logType, username,lockname, keyname,offset, limit);
			int count = deviceLogService.deviceLogListCount(groupid,beginTime, endTime, logType, username,lockname,keyname);
			PageInfo p = new PageInfo();
			p.setLimit(limit);
			p.setPageNum(page);
			p.setTotal(count);
			return new Result(p, list);
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
	

	@RequestMapping(value = "/devicelogupload")
	@ResponseBody
	public Result deviceLogUpload(@RequestBody TDeviceLogVo deviceLogVo,HttpServletRequest req, HttpServletResponse resp) {
		TUserVo tUserVo = (TUserVo) req.getSession().getAttribute("tUserVo");
		String groupid= tUserVo.getGroupid();
		try {
			deviceLogVo.setGroupid(groupid);
			Result result= deviceLogService.deviceLogUpload(deviceLogVo);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return Result.build(500, ResourcesUtil.getValue(Config.MESSAGE, "500"));
		}
	}
}
